package com.dankegongyu.lib.common.util;

import android.os.Build;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.dankegongyu.lib.common.R;

import androidx.annotation.StringRes;

/**
 * Desc: Toast工具类
 * Author: wpq
 * Date: 2017/4/1 16:05
 * <p>
 * Updata:yejiurui
 * Date:2019/11/28 19:00
 * <p>
 * DKToast.show(); 单实例Toast
 * DKToast.showInsToast(); 多实例Toast
 * DKToast.showSysToast(); 系统原生Toast
 */

public final class DKToast {

    private static Toast sToast;

    private DKToast() {
        throw new AssertionError("cannot be instantiated");
    }

    /**
     * 默认单例
     */
    public static void show(@StringRes int resId) {
        show(resId, Gravity.BOTTOM);
    }

    public static void show(@StringRes int resId, int gravity) {
        show(resId, gravity, Toast.LENGTH_SHORT);
    }

    public static void show(@StringRes int resId, int gravity, int duration) {
        show(GlobalContext.getAppContext().getResources().getString(resId), gravity, duration);
    }

    public static void show(CharSequence text) {
        show(text, Gravity.BOTTOM);
    }

    public static void show(CharSequence text, int gravity) {
        show(text, gravity, Toast.LENGTH_SHORT);
    }

    public static void show(CharSequence text, int gravity, int duration) {
        show(text, gravity, duration, null, false);
    }

    /**
     * 新实例
     */
    public static void showInsToast(@StringRes int resId) {
        showInsToast(resId, Gravity.BOTTOM);
    }

    public static void showInsToast(@StringRes int resId, int gravity) {
        showInsToast(resId, gravity, Toast.LENGTH_SHORT);
    }

    public static void showInsToast(@StringRes int resId, int gravity, int duration) {
        show(GlobalContext.getAppContext().getResources().getString(resId), gravity, duration);
    }

    public static void showInsToast(CharSequence text) {
        showInsToast(text, Gravity.BOTTOM);
    }

    public static void showInsToast(CharSequence text, int gravity) {
        show(text, gravity, Toast.LENGTH_SHORT);
    }

    public static void showInsToast(CharSequence text, int gravity, int duration) {
        show(text, gravity, duration, null, true);
    }

    /**
     * @param text          Toast内容
     * @param gravity       显示位置
     * @param duration      Toast时长
     * @param customView    自定义view
     * @param isNewInstance 是否需要新的实例Toast
     */
    private static void show(CharSequence text, int gravity, int duration, View customView, boolean isNewInstance) {
        if (TextUtils.isEmpty(text)) {
            return;
        }

        int yOffset = 0;
        switch (gravity) {
            case Gravity.BOTTOM:
                yOffset = dp2px(64);
                break;
            case Gravity.CENTER:
                yOffset = 0;
                break;
            case Gravity.TOP:
                yOffset = dp2px(64);
                break;
        }

        View toastView;
        if (customView != null) {
            toastView = customView;
        } else {
            toastView = LayoutInflater.from(GlobalContext.getAppContext()).inflate(R.layout.dk_view_toast, null);
            TextView textView = toastView.findViewById(R.id.tv_toast);
            textView.setText(text);
        }
        if (!isNewInstance && sToast != null) {
            sToast.cancel();
            sToast = null;
        }
        sToast = new Toast(GlobalContext.getAppContext());
        sToast.setView(toastView);
        sToast.setGravity(gravity, 0, yOffset);
        sToast.setDuration(duration);
        show(sToast);
    }

    private static long mLastToastTime;

    private static void show(Toast toast) {
        try {
            if (Build.VERSION.SDK_INT == Build.VERSION_CODES.N_MR1) {
                // Android7.1系统Toast有bug：主线程被阻塞后吐司会报错 WindowManager$BadTokenException，反射不让用了所以简单delay一下
                if (System.currentTimeMillis() - mLastToastTime > 2500) {
                    mLastToastTime = System.currentTimeMillis();
                    toast.show();
                }
            } else {
                toast.show();
            }
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }

    /**
     * 取消toast
     */
    public static void cancel() {
        if (sToast != null) {
            sToast.cancel();
            sToast = null;
        }
    }

    private static int dp2px(@SuppressWarnings("SameParameterValue") float dpValue) {
        float scale = GlobalContext.getAppContext().getResources().getDisplayMetrics().density;
        return (int) (scale * dpValue + 0.5f);
    }

    /**
     * 网络错误提示
     */
    public static void showNetworkErrorToast() {
        showNetworkErrorToast(Gravity.BOTTOM);
    }

    /**
     * 网络错误提示
     */
    public static void showNetworkErrorToast(int gravity) {
        show(GlobalContext.getAppContext().getResources().getString(R.string.DKToast_network_error), gravity);
    }

    /**
     * 系统自带的Toast
     */
    public static void showSysToast(CharSequence text) {
        showSysToast(text, Toast.LENGTH_SHORT);
    }

    /**
     * 系统自带的Toast
     */
    public static void showSysToast(CharSequence text, int duration) {
        Toast.makeText(GlobalContext.getAppContext(), text, Toast.LENGTH_SHORT).show();
    }
}
